home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 2000 July / macformat-092.iso / Dreamweaver 3 / Configuration / Commands / Create Web Photo Album.js < prev    next >
Encoding:
Text File  |  1999-12-01  |  16.2 KB  |  560 lines

  1. // Create Web Photo Album v 1.0
  2. // Copyright 1999 Macromedia, Inc. All rights reserved
  3.  
  4. //---------------   GLOBAL VARIABLES   ---------------
  5.  
  6. var gOpenableExtensions = [ ".gif", ".jpg", ".jpeg", ".png", ".psd", ".tif", ".tiff" ];
  7. var gHelpRelNotesURL = "/Help/html/extensions_help/create_photo_album.htm";
  8.  
  9. var gFolderURL = "";
  10. var gCancelClicked = false;
  11. var gProgressTracker = null;
  12. var gResponse = null;
  13.  
  14. var gThumbnailSize = 32;  // Default pixel size.
  15.  
  16. //---------------     API FUNCTIONS    ---------------
  17.  
  18.  
  19. function commandButtons()
  20. {
  21.   return new Array(
  22.                    BTN_OK,"buildIt()",
  23.                    BTN_Cancel,"readyToCancel();window.close()",
  24.                    BTN_Help,"displayHelp()"
  25.                   );
  26. }
  27.  
  28. function displayHelp()
  29. {
  30.   var rootPath = dw.getConfigurationPath();
  31.   var idPath = rootPath.substring(0,rootPath.lastIndexOf("/")) + gHelpRelNotesURL;
  32.   // Change the argument when you have a real URL for the release notes
  33.   dw.browseDocument(idPath);
  34. }
  35.  
  36. //---------------    LOCAL FUNCTIONS   ---------------
  37.  
  38. function initUI()
  39. {
  40.   MM.mmCWPA_openFile = null;  // Initialize the index file open parameter.
  41.   if (FWLaunch.validateFireworks(3.0)){
  42.     document.Layer1.visibility = "visible";
  43.     findObject('sitename').focus();
  44.     findObject('sitename').select();
  45.   } else {
  46.     document.Layer2.visibility = "visible";
  47.   }
  48. }
  49.  
  50. function buildIt()
  51. {
  52.   if (gProgressTracker != null) return;
  53.  
  54.   MM.setBusyCursor();
  55.   if (!checkForErrors()) {
  56.  
  57.     gThumbnailSize = findObject('thumbsize').options[findObject('thumbsize').selectedIndex].value;
  58.     gSiteName = findObject('sitename').value;
  59.     gAuthor = findObject('author').value;
  60.     gDateField = findObject('datefield').value;
  61.  
  62.     gSourceField = findObject('folder').value;
  63.     gDestination = findObject('destination').value;
  64.     gNumCols = findObject('columns').value;
  65.  
  66.     // make sure we have a unique name for the index file, so that we 
  67.     // don't overwrite any that might already be there...
  68.     for (var suffix = 0; suffix < 1000; suffix++) {
  69.       var suffixString = (suffix == 0) ? "" : suffix.toString();
  70.       gDestinationIndexFileName = "index" + suffixString + ".htm";
  71.       if (!DWfile.exists(gDestination + gDestinationIndexFileName))
  72.         break;
  73.     }
  74.  
  75.     // this is where the "gif websnap 128" etc. is set.
  76.     var fldThumb = findObject('thumbformat');
  77.     gExportFormatSelection = fldThumb.options[fldThumb.selectedIndex].value;
  78.     var fldFormat = findObject('sourceimageformat');
  79.     gSourceFormatSelection = fldFormat.options[fldFormat.selectedIndex].value;
  80.     gSourcePercentScale = findObject('imgsize').value;
  81.     
  82.     if (gExportFormatSelection == "gifwebsnap128") {
  83.       gExportExtension = ".gif";
  84.       gExportFormat = {
  85.         applyScale:true,
  86.         colorMode:"indexed",
  87.         exportFormat:"GIF",
  88.         jpegQuality:80,
  89.         jpegSmoothness:0,
  90.         jpegSubsampling:1,
  91.         name:"GIF WebSnap 128",
  92.         numEntriesRequested:128,
  93.         percentScale:100,
  94.         useScale:false,
  95.         xSize:-gThumbnailSize,
  96.         ySize:-gThumbnailSize
  97.       };
  98.     
  99.     } else if (gExportFormatSelection == "gifwebsnap256") {
  100.       gExportExtension = ".gif";
  101.       gExportFormat = {
  102.         applyScale:true,
  103.         colorMode:"indexed",
  104.         exportFormat:"GIF",
  105.         jpegQuality:80,
  106.         jpegSmoothness:0,
  107.         jpegSubsampling:1,
  108.         name:"GIF WebSnap 256",
  109.         numEntriesRequested:256,
  110.         percentScale:100,
  111.         useScale:false,
  112.         xSize:-gThumbnailSize,
  113.         ySize:-gThumbnailSize
  114.       };
  115.     
  116.     } else if (gExportFormatSelection == "jpegbetterquality") {
  117.       gExportExtension = ".jpg";
  118.       gExportFormat = {
  119.         applyScale:true,
  120.         colorMode:"24 bit",
  121.         exportFormat:"JPEG",
  122.         jpegQuality:80,
  123.         jpegSmoothness:0,
  124.         jpegSubsampling:0,
  125.         name:"JPEG - Better Quality",
  126.         numEntriesRequested:0,
  127.         percentScale:100,
  128.         useScale:false,
  129.         xSize:-gThumbnailSize,
  130.         ySize:-gThumbnailSize
  131.       };
  132.     
  133.     } else if (gExportFormatSelection == "jpegsmallerfile") {
  134.       gExportExtension = ".jpg";
  135.       gExportFormat = {
  136.         applyScale:true,
  137.         colorMode:"24 bit",
  138.         exportFormat:"JPEG",
  139.         jpegQuality:60,
  140.         jpegSmoothness:2,
  141.         jpegSubsampling:1,
  142.         name:"JPEG - Smaller File",
  143.         numEntriesRequested:0,
  144.         percentScale:100,
  145.         useScale:false,
  146.         xSize:-gThumbnailSize,
  147.         ySize:-gThumbnailSize
  148.       };
  149.     }
  150.     
  151.     if (gSourceFormatSelection == "gifwebsnap128") {
  152.       gSourceExtension = ".gif";
  153.       gSourceFormat = {
  154.         applyScale:false,
  155.         colorMode:"indexed",
  156.         exportFormat:"GIF",
  157.         jpegQuality:80,
  158.         jpegSmoothness:0,
  159.         jpegSubsampling:1,
  160.         name:"GIF WebSnap 128",
  161.         numEntriesRequested:128,
  162.         percentScale:gSourcePercentScale,
  163.         useScale:true,
  164.         xSize:0,
  165.         ySize:0
  166.       };
  167.     
  168.     } else if (gSourceFormatSelection == "gifwebsnap256") {
  169.       gSourceExtension = ".gif";
  170.       gSourceFormat = {
  171.         applyScale:false,
  172.         colorMode:"indexed",
  173.         exportFormat:"GIF",
  174.         jpegQuality:80,
  175.         jpegSmoothness:0,
  176.         jpegSubsampling:1,
  177.         name:"GIF WebSnap 256",
  178.         numEntriesRequested:256,
  179.         percentScale:gSourcePercentScale,
  180.         useScale:true,
  181.         xSize:0,
  182.         ySize:0
  183.       };
  184.     
  185.     } else if (gSourceFormatSelection == "jpegbetterquality") {
  186.       gSourceExtension = ".jpg";
  187.       gSourceFormat = {
  188.         applyScale:false,
  189.         colorMode:"24 bit",
  190.         exportFormat:"JPEG",
  191.         jpegQuality:80,
  192.         jpegSmoothness:0,
  193.         jpegSubsampling:0,
  194.         name:"JPEG - Better Quality",
  195.         numEntriesRequested:0,
  196.         percentScale:gSourcePercentScale,
  197.         useScale:true,
  198.         xSize:0,
  199.         ySize:0
  200.       };
  201.     
  202.     } else if (gSourceFormatSelection == "jpegsmallerfile") {
  203.       gSourceExtension = ".jpg";
  204.       gSourceFormat = {
  205.         applyScale:false,
  206.         colorMode:"24 bit",
  207.         exportFormat:"JPEG",
  208.         jpegQuality:60,
  209.         jpegSmoothness:2,
  210.         jpegSubsampling:1,
  211.         name:"JPEG - Smaller File",
  212.         numEntriesRequested:0,
  213.         percentScale:gSourcePercentScale,
  214.         useScale:true,
  215.         xSize:0,
  216.         ySize:0
  217.       };
  218.   
  219.     } else {
  220.       alert(MSG_GENERIC_ERROR);
  221.     }
  222.     gCreatePages = findObject('createpages').checked;
  223.     buildFiles();
  224.   }
  225.   MM.clearBusyCursor();
  226. }
  227.  
  228. function isExistingFolder(path)
  229. {
  230.     if (path == null || path == "")
  231.         return false;
  232.     
  233.     if (path.indexOf("file://") == -1)
  234.         return false;
  235.  
  236.     // DWfile.getAttributes() does not like directory names to end in a slash, 
  237.     // so remove one if there's one there...
  238.     if (path.charAt(path.length - 1) == "/")
  239.         path = path.substr(0, path.length - 1);
  240.  
  241.     var str = DWfile.getAttributes(path);
  242.     return (str != null && (str.indexOf("D") != -1));
  243. }
  244.  
  245. function checkForErrors()
  246. {
  247.   var alertMsg = "";
  248.   
  249.   if (!FWLaunch.validateFireworks(3.0)) {
  250.     alertMsg += MSG_CHECK_FW3;
  251.   }
  252.   if (findObject('sitename').value == "") {
  253.     alertMsg += MSG_CHECK_TITLE;
  254.   }
  255.   if (!isExistingFolder(findObject('folder').value)) {
  256.     alertMsg += MSG_CHECK_SRC;
  257.   }
  258.   if (!isExistingFolder(findObject('destination').value)) {
  259.     alertMsg += MSG_CHECK_DST;
  260.   }
  261.   if (findObject('columns').value == "") {
  262.     alertMsg += MSG_CHECK_COL;
  263.   }
  264.   if (findObject('createpages').checked == true && findObject('imgsize') == "") {
  265.     alertMsg += MSG_CHECK_SCALE;
  266.   }
  267.   if (alertMsg) {
  268.     alert(alertMsg);
  269.   }
  270.  
  271.   return (alertMsg != '');
  272. }
  273.  
  274. function getFilenameExtension(path)
  275. {
  276.     var extension = "";
  277.     var curlength = path.length;
  278.     for (var i = 1; i < curlength; i++) {
  279.         if (path.charAt(curlength - i) == ".") {
  280.             extension = path.substr(curlength - i);
  281.             break;
  282.         }
  283.     }
  284.     return extension;
  285. }
  286.  
  287. function isImageFile(path)
  288. {
  289.     // Uses global gOpenableExtensions
  290.     var ext = getFilenameExtension(path).toLowerCase();
  291.     for (var i in gOpenableExtensions) {
  292.         if (ext == gOpenableExtensions[i])
  293.             return true;
  294.     }
  295.     return false;
  296. }
  297.  
  298. //Passed a string, finds special chars '"\ and escapes them with \
  299.  
  300. function escQuotes(theStr){
  301.   var i, theChar, escStr = "";
  302.   for(var i=0; i<theStr.length; i++) {
  303.     theChar = theStr.charAt(i);
  304.     escStr += (theChar=='"' || theChar=="'" || theChar=="\\")?("\\"+theChar):theChar;
  305.   }
  306.   return escStr;
  307. }
  308.  
  309. function buildFiles()
  310. {
  311.   var stringToAdd = "";
  312.  
  313.   if (!gSourceField) {
  314.     alert(MSG_CHECK_SRC);
  315.     return;
  316.   }
  317.  
  318.   var fwstringToExecute = "";
  319.   if (gSiteName != "" || gAuthor != "" || gDateField != ""){
  320.     var dwstringToAdd = '<center>\n<table bgcolor="lightgrey" border=0>\n';
  321.       dwstringToAdd += (gSiteName)?'<tr>\n<td><h1>' + gSiteName + '</h1></td>\n</tr>':'';
  322.       dwstringToAdd += (gAuthor)?'\n\n<tr>\n<td><h4>' + gAuthor + '</h4></td>\n</tr>':'';
  323.       dwstringToAdd += (gDateField)?'\n\n<tr>\n<td>' + gDateField + '</td>\n</tr>':'';
  324.       dwstringToAdd += '</table>';
  325.   }
  326.  
  327.   var rawFileList = DWfile.listFolder(gSourceField, "files");
  328.  
  329.   // build a file list that contains only likely image files -- no point in thumbnailing .htm files!
  330.   var fileList = new Array();
  331.   var fileListCount = 0;
  332.   var bFilenameToLong = false;
  333.   for (i = 0; i < rawFileList.length; i++) {
  334.     if (isImageFile(rawFileList[i])) {
  335.       if (navigator.platform == "MacPPC" && rawFileList[i].length >= 27) {
  336.         bFilenameToLong = true;
  337.       } else {
  338.         fileList[fileListCount++] = rawFileList[i];
  339.       }
  340.     }
  341.   }
  342.   if (bFilenameToLong) { alert(MSG_FileNameToLong); }
  343.   if (fileList == null || fileList.length == 0) { alert(MSG_NoFilesFound); }
  344.   
  345.   if (fileList != null && fileList.length > 0) {
  346.  
  347.     DWfile.createFolder(gDestination + DIR_THUMBS + "/");
  348.     DWfile.createFolder(gDestination + DIR_IMAGES + "/");
  349.     DWfile.createFolder(gDestination + DIR_PAGES + "/");
  350.  
  351.     var jsfFileURL = gDestination + DIR_THUMBS + "/buildPhotoAlbum.jsf";
  352.     DWfile.write(jsfFileURL, "");
  353.  
  354.     dwstringToAdd += '<br>\n<table align=center border=0 cellspacing=10>\n<tr valign="bottom">';
  355.     colCounter = 0;
  356.  
  357.     // generate future pathnames for "prev" and "next" links
  358.     futureLinkArray = new Array();
  359.     for (j = 0; j < fileList.length; j++) {
  360.       futureLinkArray[j] = fileList[j].replace(/[\.]+/gi, "_") + ".htm";
  361.     }
  362.  
  363.     fwstringToExecute += "App.progressCountTotal = " + fileList.length + ";\n";
  364.     fwstringToExecute += "App.progressCountCurrent = 0;\n";
  365.     fwstringToExecute += "fw.dismissBatchDialogWhenDone = true;\n";
  366.  
  367.     // now build the .jsf file and the html tables
  368.     for (i = 0; i < fileList.length; i++) {
  369.  
  370.       colCounter++;
  371.       fileName = gSourceField + fileList[i];
  372.  
  373.       newSrcFileName = gDestination + DIR_IMAGES + '/' + fileList[i].replace(/[\.]+/gi, "_") + gSourceExtension;
  374.       newFileName = gDestination + DIR_THUMBS + '/' + fileList[i].replace(/[\.]+/gi, "_") + gExportExtension;
  375.  
  376.       newSrcFileNameRelative = DIR_IMAGES + '/' + fileList[i].replace(/[\.]+/gi, "_") + gSourceExtension;
  377.       newFileNameRelative = DIR_THUMBS + '/' + fileList[i].replace(/[\.]+/gi, "_") + gExportExtension;
  378.  
  379.       fwstringToExecute += "doc = fw.openDocument('" + escQuotes(fileName) + "');\n";
  380.       fwstringToExecute += "App.progressCountCurrent = " + (i + 1) + ";\n";
  381.       fwstringToExecute += "App.batchStatusString = '" + escQuotes(fileList[i]) + "';\n";
  382.       fwstringToExecute += "fw.exportDocumentAs(doc, '" + escQuotes(newSrcFileName) + "'," + gSourceFormat.toSource() + ");\n";
  383.       fwstringToExecute += "fw.exportDocumentAs(doc, '" + escQuotes(newFileName) + "'," + gExportFormat.toSource() + ");\n";
  384.       fwstringToExecute += "fw.closeDocument(doc);\n";
  385.  
  386.       DWfile.write(jsfFileURL, fwstringToExecute, "append");
  387.       fwstringToExecute = '';
  388.  
  389.       dwstringToAdd +='<td align="center">';
  390.  
  391.  
  392.       // do I need to make new pages?
  393.       if (gCreatePages) {
  394.         tempText = '<html>\n<head>\n<title>';
  395.         tempText += gSiteName + ' / ' + fileList[i];
  396.         tempText += '</title>\n</head>\n<body bgcolor="#ffffff">\n';
  397.         tempText += '<table border=0>\n<tr>\n<td align="left">';
  398.         tempText += '<h2>' + gSiteName + '/' + fileList[i] + '</h2>\n';
  399.  
  400.         // tricky part - add links to 'future' and 'past' documents
  401.         linkPast = i - 1;
  402.         linkFuture = i + 1;
  403.         if (linkPast < 0) {
  404.           linkPast = fileList.length - 1;
  405.         }
  406.         if (linkFuture > fileList.length - 1) {
  407.           linkFuture = 0;
  408.         }
  409.  
  410.         tempText += '<a href="' + futureLinkArray[linkPast] + 
  411.           '">' + HTM_Prev + '</a> | <a href="../' + 
  412.           gDestinationIndexFileName + '">' + HTM_Home + '</a> | <a href="' + 
  413.           futureLinkArray[linkFuture] + '">' + HTM_Next + '</a>';
  414.           
  415.         tempText += "<br><br></td>\n</tr>\n\n<tr>\n";
  416.  
  417.         // show the original-size image
  418.         tempText += '<td align="center">\n';
  419.         tempText += '<br>\n';
  420.         tempText += '<a href="../' + newSrcFileNameRelative + 
  421.           '"><img src="../' + newSrcFileNameRelative + 
  422.           '" border=0></a><br>\n';    
  423.  
  424.         // finish the table
  425.         tempText += '</td>\n</tr>\n</table>\n</body>\n</html>';
  426.  
  427.         // now write to the new file
  428.         DWfile.write(gDestination + DIR_PAGES + "/" +  
  429.           fileList[i].replace(/[\.]+/gi, "_") + ".htm", tempText);
  430.  
  431.         // now append the entry to the table
  432.         dwstringToAdd += '<a href="' + DIR_PAGES + '/' +  
  433.           fileList[i].replace(/[\.]+/gi, '_') + '.htm' +  
  434.           '"><img src="' + newFileNameRelative + '" border=0></a>\n';
  435.  
  436.       } else {
  437.  
  438.         // do not make new pages, but point to the new images directory
  439.         dwstringToAdd += '<a href="' + newSrcFileNameRelative +  
  440.           '"><img src="' + newFileNameRelative + '" border=0></a>\n';
  441.       }
  442.  
  443.       if (findObject('showfilenames').checked == true) {
  444.         dwstringToAdd += '<br>\n' + fileList[i];
  445.       }
  446.  
  447.       dwstringToAdd += '</td>\n';
  448.  
  449.       if (colCounter == gNumCols) {
  450.         dwstringToAdd += (i < fileList.length-1)?'</tr>\n\n<tr valign="bottom">':'';
  451.         colCounter = 0;
  452.       }
  453.     }
  454.  
  455.     dwstringToAdd += '</tr>\n</table>\n</center>';
  456.  
  457.     DWfile.write(jsfFileURL, "'done';", "append");
  458.  
  459.     stringToAdd = newDocHTML(gSiteName, dwstringToAdd);
  460.     DWfile.write(gDestination + gDestinationIndexFileName, stringToAdd);
  461.  
  462.     // launch FW now
  463.     gProgressTracker = FWLaunch.execJsInFireworks(jsfFileURL);
  464.     if (gProgressTracker == null || typeof(gProgressTracker) == "number") {
  465.       window.close();
  466.       alert(MSG_Error);
  467.       gProgressTracker = null;
  468.     } else {
  469.       // start the checking.
  470.       checkOneMoreTime();
  471.     }
  472.   }
  473. }
  474.  
  475. function newDocHTML(docTitle, bodyHTML) {
  476.   var rtnStr = '';
  477.   if (!docTitle) docTitle = '';
  478.   if (!bodyHTML) bodyHTML = '';
  479.   
  480.   rtnStr = '<html>\n<head>\n<title>' +
  481.     docTitle +
  482.     '</title>\n' +
  483.     '<meta http-equiv="Content-Type" content="text/html; charset=">\n</head>\n' +
  484.     '<body bgcolor="#FFFFFF">\n' +
  485.     bodyHTML + '\n' +
  486.     '</body>\n</html>';
  487.     
  488.   return rtnStr;
  489. }
  490.  
  491.  
  492. function checkOneMoreTime()
  493. {
  494.   window.setTimeout("checkJsResponse();", 500);
  495. }
  496.  
  497. function checkJsResponse()
  498. {
  499.   if (gCancelClicked) {
  500.     window.close();
  501.   } else {
  502.     if (gProgressTracker != null) gResponse = FWLaunch.getJsResponse(gProgressTracker);
  503.  
  504.     if (gResponse == null) {
  505.       // still waiting for a gResponse.
  506.       checkOneMoreTime();
  507.       
  508.     } else if (typeof(gResponse) == "number") {
  509.       // error or user-cancel, time to punt
  510.       window.close();
  511.       alert(MSG_GENERIC_ERROR);
  512.       
  513.     } else if (gResponse == 'done') {
  514.       // got a gResponse!      
  515.  
  516.       FWLaunch.bringDWToFront();
  517.       alert(MSG_SUCCESS);
  518.       // Remember the index page that will be opened from menus.xml.
  519.       MM.mmCWPA_openFile = gDestination + gDestinationIndexFileName;
  520.       window.close();
  521.  
  522.     } else {
  523.       FWLaunch.bringDWToFront();
  524.       alert(MSG_FailOrCancel);
  525.       window.close();
  526.     }
  527.   }
  528. }
  529.  
  530. function readyToCancel()
  531. {
  532.   gCancelClicked = true;
  533. }
  534.  
  535.  
  536. //---------------   GENERIC FUNCTIONS  ---------------
  537.  
  538. function browseFile(fldName)
  539. {
  540.   var fileName;
  541.   var curFld = findObject(fldName);
  542.   var selectedDir = curFld.value;
  543.   
  544.   theSite = dreamweaver.getSiteRoot();
  545.   if (DWfile.exists(selectedDir)) {
  546.     fileName = dw.browseForFolderURL(MSG_CHOOSEFOLDER, curFld.value);
  547.   } else {
  548.     fileName = dw.browseForFolderURL(MSG_CHOOSEFOLDER);
  549.   }
  550.   
  551.   if (fileName) {
  552.     if (fileName.indexOf("file://") != -1) {
  553.       curFld.value = fileName;
  554.     } else {
  555.       curFld.value = theSite + fileName;
  556.     }
  557.   }
  558. }
  559.  
  560.